﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;

using Ext.Net;
using ZP.Data;
using Kwan.Data;
using ZP.Service;

namespace ZP.Web.Admin.Company
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                IndtypeDS.DataSource = new Expression().List<TIndustry>();
                IndtypeDS.DataBind();
            }
        }

        protected void BtnAddInfo_Click(object sender, EventArgs e)
        {
            NewEditWindow();
            EditWindow.Show();
        }

        [DirectMethod]
        public void DeleteSelected(string obj)
        {
            string[] idList = obj.Split(',');
            (TCompany.entityID == idList).Delete<TCompany>();
            Store1.DataBind();
        }

        private void DeleteComJob(int id)
        {
            (TJob.companyID == id).Delete<TJob>();
        }

        [DirectMethod]
        public void ChangeState(string obj, string NameValue)
        {
            CrudManage.ChangeSelected(obj, NameValue, "ZP_Company");
            Store1.DataBind();
        }

        [DirectMethod]
        public void EditInfo(string id)
        {
            SetEditWindow(DBContext.Load<VCompany>(int.Parse(id)));
            EditWindow.Show();
        }

        protected void RefreshDS(object sender, StoreRefreshDataEventArgs e)
        {
            NameValueCollection ps = new NameValueCollection();
            ps.Set("gridsort", e.Sort);
            ps.Set("dir", e.Dir.ToString());
            
            //搜索项
            ps.Set("CompanyName", txtSearch.Text);
            ps.Set("IndustryCode", searchIndtype.Text);

            int pageindex = e.Start / e.Limit;
            Kwan.Data.Region r = new Kwan.Data.Region(pageindex, e.Limit);

            Expression exp = ArticleQuery.MakeSearchWhere(ps);
            IList<VCompany> ListView = exp.List<VCompany>(r, new string[] { e.Sort + " " + e.Dir });

            e.Total = new Expression().Count<TCompany>(); 
            
            Store1.DataSource = ListView;
            Store1.DataBind();
        }

        protected void NewEditWindow()
        {
            txtID.Text = string.Empty;
            txtScale.Checked = true;
			txtAddress.Text = string.Empty;
			txtCompanyName.Text = string.Empty;
			txtContactPerson.Text = string.Empty;
			txtCreatedDate.Text = string.Empty;
			txtEmail.Text = string.Empty;
			txtEmployees.Text = string.Empty;
			txtIndtype.Text = string.Empty;
			txtMemo.Text = string.Empty;
			txtType.Text = string.Empty;
        }

        protected void SetEditWindow(VCompany tt)
        {
            txtID.Text = tt.EntityID.ToString();
            txtScale.Checked = tt.Scale == 1;
			txtAddress.Text = tt.Address;
			txtCompanyName.Text = tt.CompanyName;
			txtContactPerson.Text = tt.ContactPerson;
			txtCreatedDate.Text = tt.CreatedDate.ToString();
			txtEmail.Text = tt.Email;
			txtEmployees.Text = tt.Employees;
			txtIndtype.Text = tt.IndustryName;
			txtMemo.Text = tt.Memo;
			txtType.Text = tt.Type.ToString();
        }

        protected void SaveEditWindow(object sender, DirectEventArgs e)
        {
            TCompany tt = !String.IsNullOrEmpty(txtID.Text) ? DBContext.Load<TCompany>(Convert.ToInt32(txtID.Text)) : new TCompany();

			tt.Address = txtAddress.Text;
			tt.CompanyName = txtCompanyName.Text;
			tt.ContactPerson = txtContactPerson.Text;
			tt.Email = txtEmail.Text;
			tt.Employees = txtEmployees.Text;
			tt.IndustryCode = Convert.ToInt32(txtIndtype.Value);
			tt.Memo = txtMemo.Text;
            tt.ModifiedDate = DateTime.Now;
			//tt.Modifier = txtModifier.Text;
			tt.Type = Convert.ToInt32(txtType.Text);
            tt.Scale = txtScale.Checked ? 1 : 0;

            DBContext.Save(tt);

            Store1.DataBind();
            EditWindow.Hide();
        }
    }
}